package com.sk.bilibili.view.fragment.them;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.sk.bilibili.adapter.vlayout.HorizontalAdapter;
import com.sk.bilibili.adapter.vlayout.them.introduction.ChooseAdapter;
import com.sk.bilibili.adapter.vlayout.them.introduction.HeaderAdapter;
import com.sk.bilibili.adapter.vlayout.them.introduction.OperationAdapter;
import com.sk.bilibili.adapter.vlayout.them.introduction.RecommendAdapter;
import com.sk.bilibili.adapter.vlayout.them.introduction.SelectionAdapter;
import com.sk.bilibili.bean.FollowThem;
import com.sk.bilibili.bean.Them;
import com.sk.bilibili.bean.Video;
import com.sk.bilibili.bean.VideoSet;
import com.sk.bilibili.event.VideoSetEvent;
import com.sk.bilibili.model.fragment.them.IntroductionModel;
import com.sk.bilibili.view.activity.AppraiseActivity;
import com.sk.bilibili.view.activity.DynamicActivity;
import com.sk.bilibili.view.activity.MemberActivity;
import com.sk.bilibili.view.activity.ThemActivity;
import com.sk.bilibili.view.diy.bottomDialog.FollowBottomDialog;
import com.sk.bilibili.view.fragment.BaseRefreshFragment;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

public class IntroductionFragment extends BaseRefreshFragment<IntroductionModel> {

    private Integer themId;
    private FollowBottomDialog bottomDialog;

    public IntroductionFragment(Integer themId) {
        this.themId=themId;
        bottomDialog = new FollowBottomDialog();
    }

    private HeaderAdapter headerAdapter;
    private OperationAdapter operationAdapter;
    private ChooseAdapter chooseAdapter;
    private HorizontalAdapter<Video> horizontalAdapter;
    private RecommendAdapter recommendAdapter;
    private FollowThem followThem;

    @Override
    protected void initAdapter(DelegateAdapter adapter) {
        headerAdapter = new HeaderAdapter(getContext(), this);
        adapter.addAdapter(headerAdapter);
        operationAdapter = new OperationAdapter(getContext(), this);
        adapter.addAdapter(operationAdapter);
        chooseAdapter = new ChooseAdapter(getContext());
        adapter.addAdapter(chooseAdapter);
        horizontalAdapter = new HorizontalAdapter<>(getContext(),
                new SelectionAdapter(getContext(), this));
        adapter.addAdapter(horizontalAdapter);
        recommendAdapter = new RecommendAdapter(getContext(), this);
        adapter.addAdapter(recommendAdapter);
    }

    @Override
    public void onRefresh() {
        model.refreshData(themId);
        model.refreshData();
    }

    public void onInfoClick(Them them) {

    }

    public void onFollowClick() {
        if(followThem==null){
            model.followThem(themId);
        }else {
            bottomDialog.show(getChildFragmentManager(),followThem.getId());
        }
    }

    public void loadThemSuccess(Them them) {
        followThem = them.getExtra().getFollowThem();
        VideoSet videoSet = them.getExtra().getVideoSet();
        headerAdapter.setData(them);
        operationAdapter.setData(videoSet);
        chooseAdapter.setData(them);
        if(videoSet.getExtra().getVideos().size()>1){
            horizontalAdapter.setList(videoSet.getExtra().getVideos());
        }
        cancelRefresh();
    }

    public void onCoinClick() {
        model.addCoin(themId);
    }

    public void onReviewClick() {
        AppraiseActivity.start(getContext(),themId);
    }

    public void onForwardClick() {
        DynamicActivity.start(getContext(),1,themId);
    }

    public void onSelectionClick(Video video) {
        EventBus.getDefault().post(VideoSetEvent.changeVideo(video));
    }

    public void onThemClick(Integer id) {
        ThemActivity.start(getContext(),id);
    }

    public void loadRecommendSuccess(List<Them> list) {
        recommendAdapter.setData(list);
    }

    public void followThemSuccess() {
        toast("自己追的番一定要看完欧");
    }

    public void addCoinSuccess() {
        toast("投币成功");
    }

    public void addCoinError(String message) {
        toast(message);
    }

}